*&---------------------------------------------------------------------*
*& 包含               ZFIU021_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form frm_init_screen
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_INIT_SCREEN .
  DATA LV_FUNCTXT TYPE SMP_DYNTXT.
  LV_FUNCTXT-ICON_ID       = ICON_EXPORT.
  LV_FUNCTXT-QUICKINFO     = TEXT-F01.
  LV_FUNCTXT-ICON_TEXT     = TEXT-F01.

  SSCRFIELDS-FUNCTXT_01 = LV_FUNCTXT.


  EKKO-BSTYP = 'F'. "采购凭证类别

*  S_ZKPSJ-HIGH = SY-DATUM.
*  S_ZKPSJ-LOW = SY-DATUM - 30.
*  APPEND S_ZKPSJ.

*  S_AEDAT-HIGH = SY-DATUM.
*  S_AEDAT-LOW = SY-DATUM - 30.
*  APPEND S_AEDAT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_download_data
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_GET_DATA .
  DATA:
    LV_TABIX        TYPE I,
    LV_MSG          TYPE STRING,
    LV_JSDDH        TYPE ZFIT079_HEAD-JSDDH,
    LV_WAERS        TYPE ZFIT079_HEAD-WAERS,
    LT_R_CHARG      TYPE RANGE OF EKBE-CHARG WITH HEADER LINE,
    LT_ZFIT078_HEAD TYPE TABLE OF ZFIT078_HEAD WITH HEADER LINE.

  IF S_LIFNR[] IS INITIAL AND S_ZFPHM[] IS INITIAL AND
    S_ZKPSJ[] IS INITIAL  AND S_ZXFMC[] IS INITIAL.

    MESSAGE '发票和收货行查询条件，请分别录入至少一个'(M08) TYPE 'E'.
    RETURN.
  ENDIF.

  IF S_BSART[] IS INITIAL AND S_EKGRP[] IS INITIAL AND
    S_EBELN[] IS INITIAL AND S_EBELP[] IS INITIAL AND
    S_WERKS[] IS INITIAL AND S_MATNR[] IS INITIAL AND
    S_CHARG[] IS INITIAL AND S_LICHA[] IS INITIAL AND
    S_ERNAM[] IS INITIAL AND S_AEDAT[] IS INITIAL AND
    S_GJAHR[] IS INITIAL AND S_BELNR[] IS INITIAL AND
    S_EBENP[] IS INITIAL .

    MESSAGE '发票和收货行查询条件，请分别录入至少一个'(M08) TYPE 'E'.
    RETURN.
  ENDIF.

  "获取发票数据
  SELECT *
    INTO TABLE LT_ZFIT078_HEAD
    FROM ZFIT078_HEAD
   WHERE ZFPHM IN S_ZFPHM
     AND LIFNR IN S_LIFNR
     AND BUKRS IN S_BUKRS
     AND ZKPSJ IN S_ZKPSJ
     AND ZXFMC IN S_ZXFMC.
  IF SY-SUBRC <> 0.
    MESSAGE TEXT-M01 TYPE 'E'.
    RETURN.
  ENDIF.

  LOOP AT LT_ZFIT078_HEAD.
    CONDENSE: LT_ZFIT078_HEAD-ZGFMC.
    IF LT_ZFIT078_HEAD-ZGFMC <> '华菱安赛乐米塔尔汽车板有限公司'.
      LV_MSG = '发票&购方名称不正确，请检查'(M21).
      REPLACE ALL OCCURRENCES OF '&' IN LV_MSG WITH LT_ZFIT078_HEAD-ZFPHM.
      MESSAGE LV_MSG TYPE 'E'.
      RETURN.
    ENDIF.
  ENDLOOP.

  "排除结算单中已经存在的发票号码
  SELECT
    A~JSDDH,
    B~JSDHH,
    B~ZYSFPHM
    INTO TABLE @DATA(LT_JSD_EXIST)
    FROM ZFIT079_HEAD AS A
    JOIN ZFIT079_ITEM AS B
      ON A~JSDDH = B~JSDDH
     FOR ALL ENTRIES IN @LT_ZFIT078_HEAD
   WHERE B~ZYSFPHM = @LT_ZFIT078_HEAD-ZFPHM
     AND A~ZYWLX <> @GC_ZYWLX_FOREIGN
     AND A~JSDZT <> @GC_JSDZT_DEPRECATED.

  SORT LT_JSD_EXIST BY ZYSFPHM.
  DELETE ADJACENT DUPLICATES FROM LT_JSD_EXIST COMPARING ZYSFPHM.

  LOOP AT LT_ZFIT078_HEAD.
    LV_TABIX = SY-TABIX.

    READ TABLE LT_JSD_EXIST TRANSPORTING NO FIELDS
      WITH KEY ZYSFPHM = LT_ZFIT078_HEAD-ZFPHM
      BINARY SEARCH.
    IF SY-SUBRC = 0 .
      DELETE LT_ZFIT078_HEAD INDEX LV_TABIX.
    ENDIF.
  ENDLOOP.
  IF LT_ZFIT078_HEAD[] IS INITIAL.
    MESSAGE TEXT-M01 TYPE 'E'.
    RETURN.
  ENDIF.

  SORT LT_ZFIT078_HEAD BY BUKRS LIFNR ZXFMC ZKPSJ ZFPHM.
  LOOP AT LT_ZFIT078_HEAD.
    CLEAR GT_INV_DATA.
    MOVE-CORRESPONDING LT_ZFIT078_HEAD TO GT_INV_DATA.
    IF LT_ZFIT078_HEAD-LIFNR IS NOT INITIAL.
      GT_INV_DATA-ZXFMC = LT_ZFIT078_HEAD-LIFNR && GC_CONCAT_FLAG && LT_ZFIT078_HEAD-ZXFMC.
    ENDIF.

    APPEND GT_INV_DATA.
  ENDLOOP.

  IF S_CHARG[] IS NOT INITIAL OR S_LICHA[] IS NOT INITIAL.
    PERFORM FRM_ADD_CHARG(ZFIU019) IF FOUND
      USING S_CHARG[] S_LICHA[] CHANGING LT_R_CHARG[].
  ENDIF.

  IF P_XBNKYF IS NOT INITIAL.
    REFRESH S_LIFNR[].
  ENDIF.

  "获取收货数据
  PERFORM FRM_GET_DATA_GR(ZFIU019) IF FOUND
     USING
        'X' "排除结算单中存在的物料凭证
        LV_JSDDH
        LV_WAERS
        S_BUKRS[]
        S_LIFNR[]
        S_BSART[]
        S_EKGRP[]
        S_EBELN[]
        S_EBELP[]
        S_EBENP[]
        S_WERKS[]
        S_MATNR[]
        S_GJAHR[]
        S_BELNR[]
        LT_R_CHARG[]
        S_ERNAM[]
        S_AEDAT[]
        '1'  "Invoice
        P_XWARE
        P_XBNKYF
        P_XBNKQT
        P_ZBL
    CHANGING GT_GR_QUERY_RESULT[] GS_MSG.

  IF GS_MSG-TYPE = 'E'.
    MESSAGE GS_MSG-MESSAGE TYPE 'E'.
    RETURN.
  ENDIF.

  IF GS_MSG-TYPE = 'W'.
    MESSAGE GS_MSG-MESSAGE TYPE 'W'.
  ENDIF.

  SORT GT_GR_QUERY_RESULT BY LIFNR EBELN EBELP MBLNR MJAHR ZEILE SHEET_NO COND_TYPE.

  LOOP AT GT_GR_QUERY_RESULT.
    CLEAR GT_GR_DATA.
    MOVE-CORRESPONDING GT_GR_QUERY_RESULT TO GT_GR_DATA.

    GT_GR_DATA-LIFNR_AND_NAME = GT_GR_QUERY_RESULT-LIFNR && GC_CONCAT_FLAG  && GT_GR_QUERY_RESULT-LIFNR_NAME.
    GT_GR_DATA-BSART_AND_TEXT = GT_GR_QUERY_RESULT-BSART && GC_CONCAT_FLAG  && GT_GR_QUERY_RESULT-BATXT.
    GT_GR_DATA-WERKS_AND_NAME = GT_GR_QUERY_RESULT-WERKS && GC_CONCAT_FLAG  && GT_GR_QUERY_RESULT-WERKS_NAME.
    APPEND GT_GR_DATA.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILL_DATA_TO_EXCEL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_FILL_DATA_TO_EXCEL .

  DATA:
    LV_TITLE     TYPE STRING,
    LV_DIR       TYPE STRING,
    LV_ROW       TYPE I,
    LV_COLUMN    TYPE I,
    LR_DATA_DESC TYPE REF TO CL_ABAP_DATADESCR,
    LV_ABAP_TYPE TYPE  ABAP_TYPEKIND.

  DATA LS_KEY TYPE WWWDATATAB.
  DATA LT_MIME TYPE TABLE OF W3MIME.
  DATA LV_XSTRING TYPE XSTRING.
  DATA LO_READER TYPE REF TO ZIF_EXCEL_READER.
  DATA LO_EXCEL_WRITER TYPE REF TO ZIF_EXCEL_WRITER.
  DATA LO_WORKSHEET TYPE REF TO ZCL_EXCEL_WORKSHEET.
  DATA LV_FILE TYPE XSTRING.
  DATA LV_BYTECOUNT TYPE I.
  DATA LT_FILE_TAB  TYPE SOLIX_TAB.

  SELECT SINGLE
    A~RELID,
    A~OBJID,
    B~VALUE
    FROM WWWDATA AS A
    LEFT JOIN WWWPARAMS AS B
      ON A~RELID = B~RELID
     AND A~OBJID = B~OBJID
     AND B~NAME EQ 'filesize'
   WHERE A~SRTF2 = 0
     AND A~RELID = 'MI'
     AND A~OBJID = @GC_BATCH_TEMPL_NAME
     INTO @DATA(LS_TEMPT).
  IF SY-SUBRC <> 0 .
    MESSAGE TEXT-M03 TYPE 'E'.
    RETURN.
  ENDIF.

  LV_TITLE = '请选择数据下载目录'(T03).

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE
    EXPORTING
      WINDOW_TITLE         = LV_TITLE
    CHANGING
      SELECTED_FOLDER      = LV_DIR
    EXCEPTIONS
      CNTL_ERROR           = 1
      ERROR_NO_GUI         = 2
      NOT_SUPPORTED_BY_GUI = 3
      OTHERS               = 4.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE 'E' NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    RETURN.
  ENDIF.

  IF LV_DIR IS INITIAL.
    RETURN.
  ENDIF.

  LS_KEY = VALUE #( RELID = LS_TEMPT-RELID OBJID = LS_TEMPT-OBJID ).
  CALL FUNCTION 'WWWDATA_IMPORT'
    EXPORTING
      KEY               = LS_KEY
    TABLES
      MIME              = LT_MIME
    EXCEPTIONS
      WRONG_OBJECT_TYPE = 1
      IMPORT_ERROR      = 2
      OTHERS            = 3.
  IF SY-SUBRC <> 0.
    MESSAGE TEXT-M04 TYPE 'E'.
    RETURN.
  ENDIF.

  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      INPUT_LENGTH = CONV I( LS_TEMPT-VALUE )
    IMPORTING
      BUFFER       = LV_XSTRING
    TABLES
      BINARY_TAB   = LT_MIME
    EXCEPTIONS
      FAILED       = 1
      OTHERS       = 2.
  IF SY-SUBRC <> 0.
    MESSAGE TEXT-M04 TYPE 'E'.
    RETURN.
  ENDIF.

  CHECK LV_XSTRING IS NOT INITIAL.

  TRY .
      CREATE OBJECT LO_READER TYPE ZCL_EXCEL_READER_2007.
      DATA(LO_EXCEL) = LO_READER->LOAD( I_EXCEL2007 = LV_XSTRING ).
      LO_EXCEL->SET_ACTIVE_SHEET_INDEX( 1 ).
      LO_WORKSHEET = LO_EXCEL->GET_ACTIVE_WORKSHEET( ).

      "填充发票数据
      LOOP AT GT_INV_DATA.
        LV_ROW = SY-TABIX + 1.
        DO.
          ASSIGN COMPONENT SY-INDEX OF STRUCTURE GT_INV_DATA TO FIELD-SYMBOL(<FS_FIELD>).
          IF SY-SUBRC <> 0.
            EXIT.
          ENDIF.

          IF <FS_FIELD> IS INITIAL."为空的不再赋值
            CONTINUE.
          ENDIF.

          LV_COLUMN =  SY-INDEX.
          LR_DATA_DESC ?=  CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <FS_FIELD> ).
          LV_ABAP_TYPE = LR_DATA_DESC->GET_DATA_TYPE_KIND( <FS_FIELD> ).

          LO_WORKSHEET->SET_CELL( IP_COLUMN    = LV_COLUMN
                                  IP_ROW       = LV_ROW
                                  IP_VALUE     = <FS_FIELD>
                                  IP_ABAP_TYPE = COND #( WHEN LV_ABAP_TYPE IS NOT INITIAL THEN LV_ABAP_TYPE ELSE CL_ABAP_TYPEDESCR=>TYPEKIND_STRING ) ).
        ENDDO.
      ENDLOOP.

      "填充收货数据
      LO_EXCEL->SET_ACTIVE_SHEET_INDEX( 2 ).
      LO_WORKSHEET = LO_EXCEL->GET_ACTIVE_WORKSHEET( ).

      LOOP AT GT_GR_DATA.
        LV_ROW = SY-TABIX + 2.
        DO.
          ASSIGN COMPONENT SY-INDEX OF STRUCTURE GT_GR_DATA TO <FS_FIELD>.
          IF SY-SUBRC <> 0.
            EXIT.
          ENDIF.

          IF <FS_FIELD> IS INITIAL."为空的不再赋值
            CONTINUE.
          ENDIF.

          LV_COLUMN =  SY-INDEX + 5.
          LR_DATA_DESC ?=  CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <FS_FIELD> ).
          LV_ABAP_TYPE = LR_DATA_DESC->GET_DATA_TYPE_KIND( <FS_FIELD> ).

          LO_WORKSHEET->SET_CELL( IP_COLUMN    = LV_COLUMN
                                  IP_ROW       = LV_ROW
                                  IP_VALUE     = <FS_FIELD>
                                  IP_ABAP_TYPE = COND #( WHEN LV_ABAP_TYPE IS NOT INITIAL THEN LV_ABAP_TYPE ELSE CL_ABAP_TYPEDESCR=>TYPEKIND_STRING ) ).
        ENDDO.
      ENDLOOP.

      CREATE OBJECT LO_EXCEL_WRITER TYPE ZCL_EXCEL_WRITER_2007.
      LV_FILE = LO_EXCEL_WRITER->WRITE_FILE( LO_EXCEL ).

    CATCH ZCX_EXCEL INTO DATA(LR_CX_EXCEL).

      MESSAGE LR_CX_EXCEL->GET_TEXT( ) TYPE 'E'.
      RETURN.
  ENDTRY.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      BUFFER        = LV_FILE
    IMPORTING
      OUTPUT_LENGTH = LV_BYTECOUNT
    TABLES
      BINARY_TAB    = LT_FILE_TAB.

  LV_DIR = LV_DIR && TEXT-T04 && SY-DATUM &&  '.xlsx'. "'_' && SY-UZEIT &&

  CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD(
    EXPORTING
      BIN_FILESIZE            = LV_BYTECOUNT
      FILENAME                = LV_DIR
      FILETYPE                = 'BIN'
    CHANGING
      DATA_TAB                = LT_FILE_TAB
    EXCEPTIONS
      FILE_WRITE_ERROR        = 1
      NO_BATCH                = 2
      GUI_REFUSE_FILETRANSFER = 3
      INVALID_TYPE            = 4
      NO_AUTHORITY            = 5
      UNKNOWN_ERROR           = 6
      HEADER_NOT_ALLOWED      = 7
      SEPARATOR_NOT_ALLOWED   = 8
      FILESIZE_NOT_ALLOWED    = 9
      HEADER_TOO_LONG         = 10
      DP_ERROR_CREATE         = 11
      DP_ERROR_SEND           = 12
      DP_ERROR_WRITE          = 13
      UNKNOWN_DP_ERROR        = 14
      ACCESS_DENIED           = 15
      DP_OUT_OF_MEMORY        = 16
      DISK_FULL               = 17
      DP_TIMEOUT              = 18
      FILE_NOT_FOUND          = 19
      DATAPROVIDER_EXCEPTION  = 20
      CONTROL_FLUSH_ERROR     = 21
      NOT_SUPPORTED_BY_GUI    = 22
      ERROR_NO_GUI            = 23
      OTHERS                  = 24 ).
  IF SY-SUBRC <> 0.
    "ES_RETURN-MSGTS = 'E'.
  ENDIF.
ENDFORM.

FORM FRM_GET_TAX_RATE USING
      IV_ALAND TYPE RF82T-LAND1
      IV_TAXCODE TYPE RF82T-MWSKZ
      CHANGING CV_TAXRATE TYPE ZDT_ZFPSL
               CV_EXIST_FLG TYPE C.

  DATA:
    LV_TXJCD TYPE RF82T-TXJCD,
    LT_FTAXP TYPE TABLE OF FTAXP WITH HEADER LINE.

  CLEAR:
    CV_EXIST_FLG,
    CV_TAXRATE.

  LV_TXJCD = 'TAX' && IV_ALAND.

  IF IV_TAXCODE IS NOT INITIAL.
    "取税率
    CALL FUNCTION 'GET_TAX_PERCENTAGE'
      EXPORTING
        ALAND   = IV_ALAND
        DATAB   = SY-DATUM
        MWSKZ   = IV_TAXCODE
        TXJCD   = LV_TXJCD
*       EXPORT  = ' '
      TABLES
        T_FTAXP = LT_FTAXP[].

    LOOP AT LT_FTAXP.
      CV_TAXRATE = LT_FTAXP-KBETR / 1000 * 100.
      CV_EXIST_FLG = 'X'.
    ENDLOOP.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_GET_FILE .
  DATA:
    LT_FILE_TABLE TYPE FILETABLE,
    LS_FILE_TABLE TYPE LINE OF FILETABLE,
    LV_RC         TYPE I.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE            = 'Select Source Excel File'
*     default_extension       = ''
*     default_filename        =
      FILE_FILTER             = 'Excel Files (*.xlsx)|*.xlsx'
*     with_encoding           =
*     initial_directory       =
*     multiselection          =
    CHANGING
      FILE_TABLE              = LT_FILE_TABLE
      RC                      = LV_RC
*     user_action             =
*     file_encoding           =
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      NOT_SUPPORTED_BY_GUI    = 4
      OTHERS                  = 5.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  READ TABLE LT_FILE_TABLE INTO LS_FILE_TABLE INDEX 1.
  IF SY-SUBRC = 0.
    P_FILE = LS_FILE_TABLE-FILENAME.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILE_UPLOAD
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*

FORM FRM_GET_DATA_FROM_EXCEL.
  DATA:
    LO_READER    TYPE REF TO ZIF_EXCEL_READER,
    LO_EXCEL     TYPE REF TO ZCL_EXCEL,
    LO_WORKSHEET TYPE REF TO ZCL_EXCEL_WORKSHEET,
    LT_CELL      TYPE ZEXCEL_T_CELL_DATA.

  FIELD-SYMBOLS: <FS_FIELD> TYPE ANY.

  IF P_FILE IS INITIAL.
    MESSAGE '请选择已匹配的发票和收货行数据文件'(M06) TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  IF P_SPLNUM < 0 OR P_SPLNUM > 450.
    MESSAGE '请录入有效的拆分最大行项目数'(M23) TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  TRY .
      CREATE OBJECT LO_READER TYPE ZCL_EXCEL_READER_2007.
      LO_EXCEL = LO_READER->LOAD_FILE( I_FILENAME = P_FILE ).
      LO_EXCEL->SET_ACTIVE_SHEET_INDEX( 2 ).
      LO_WORKSHEET = LO_EXCEL->GET_ACTIVE_WORKSHEET( ).

      LT_CELL = LO_WORKSHEET->SHEET_CONTENT.

    CATCH ZCX_EXCEL INTO DATA(LR_CX_EXCEL).

      "MESSAGE LR_CX_EXCEL->GET_TEXT( ) TYPE 'S' DISPLAY LIKE 'E'.
      MESSAGE '读取文件出错，请关闭excel文件后重试'(M09) TYPE 'S' DISPLAY LIKE 'E'.
      LEAVE LIST-PROCESSING.
  ENDTRY.

  "删除表头数据
  DELETE LT_CELL WHERE CELL_ROW <= 2.
  IF LT_CELL[] IS INITIAL.
    MESSAGE '文件中无有效数据，请检查'(M07) TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  LOOP AT LT_CELL INTO DATA(LS_CELL).
    "对字段赋值，内表字段顺序要和Excel完全一致
    ASSIGN COMPONENT LS_CELL-CELL_COLUMN OF STRUCTURE GT_UPLOAD_DATA TO <FS_FIELD>.
    <FS_FIELD> = LS_CELL-CELL_VALUE.

    AT END OF CELL_ROW.
      APPEND GT_UPLOAD_DATA.
      CLEAR GT_UPLOAD_DATA.
    ENDAT.
  ENDLOOP.

  "删除没有发票号码的数据
  DELETE GT_UPLOAD_DATA WHERE ZFPHM IS INITIAL.

  IF GT_UPLOAD_DATA[] IS INITIAL.
    MESSAGE '文件中无有效数据，请检查'(M07) TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_PROCESS_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_PROCESS_DATA .
  DATA:
    LT_R_BUKRS TYPE RANGE OF EKKO-BUKRS WITH HEADER LINE,
    LT_R_LIFNR TYPE RANGE OF EKKO-LIFNR WITH HEADER LINE,
    LT_R_BSART TYPE RANGE OF EKKO-BSART WITH HEADER LINE,
    LT_R_EKGRP TYPE RANGE OF EKKO-EKGRP WITH HEADER LINE,
    LT_R_EBELN TYPE RANGE OF EKKO-EBELN WITH HEADER LINE,
    LT_R_EBELP TYPE RANGE OF EKPO-EBELP WITH HEADER LINE,
    LT_R_EBENP TYPE RANGE OF CHAR30 WITH HEADER LINE,
    LT_R_WERKS TYPE RANGE OF EKPO-WERKS WITH HEADER LINE,
    LT_R_MATNR TYPE RANGE OF EKPO-MATNR WITH HEADER LINE,
    LT_R_GJAHR TYPE RANGE OF EKBE-GJAHR WITH HEADER LINE,
    LT_R_BELNR TYPE RANGE OF EKBE-BELNR WITH HEADER LINE,
    LT_R_CHARG TYPE RANGE OF EKBE-CHARG WITH HEADER LINE,
    LT_R_ERNAM TYPE RANGE OF EKKO-ERNAM WITH HEADER LINE,
    LT_R_AEDAT TYPE RANGE OF EKKO-AEDAT WITH HEADER LINE.

  DATA:
    LV_TABIX          TYPE I,
    LV_TAXEXIST       TYPE C,
    LV_COUNT          TYPE I,
    LV_NUM2           TYPE NUMC2,
    LV_JSDDH          TYPE ZFIT079_HEAD-JSDDH,
    LV_WAERS          TYPE ZFIT079_HEAD-WAERS,
    LV_HB_ZFPHM_FIRST TYPE ZFIT079_HEAD-ZFPHM,  "合并后第一个发票号码
    LV_CF_ZFPHM_LAST  TYPE ZFIT079_HEAD-ZFPHM,  "拆分后最后一个发票号码
    LV_ZHJSE          TYPE ZFIT079_ITEM-ZFPSE,
    LV_ZBHSJE         TYPE ZFIT079_ITEM-ZFPSE,
    LV_ZBHSJE_SUM     TYPE ZFIT079_ITEM-ZFPSE,
    LV_MENGE          TYPE ZFIT079_ITEM-ERFMG,
    LV_WRBTR          TYPE ZFIT079_ITEM-WRBTR,
    LV_ZFPSE_SUM      TYPE ZFIT079_ITEM-ZFPSE,
    LV_WRBTR_SUM      TYPE ZFIT079_ITEM-WRBTR,

    LT_COLOR          TYPE LVC_T_SCOL,
    LS_COLOR          TYPE LVC_S_SCOL,
    LS_RESULT_TMP     TYPE TY_RESULT,
    LT_RESULT         TYPE TABLE OF TY_RESULT WITH HEADER LINE,    "
    LT_RESULT_TMP     TYPE TABLE OF TY_RESULT WITH HEADER LINE,    "
    LT_RESULT_HB      TYPE TABLE OF TY_RESULT WITH HEADER LINE, "合并后数据
    LT_RESULT_CF      TYPE TABLE OF TY_RESULT WITH HEADER LINE, "拆分后数据
    LT_UPLOAD_TMP     TYPE TABLE OF TY_UPLOAD_DATA WITH HEADER LINE,
    LT_UPLOAD_TMP1    TYPE TABLE OF TY_UPLOAD_DATA WITH HEADER LINE.

  CHECK GT_UPLOAD_DATA[] IS NOT INITIAL.

  "1.根据上传的文件数据，获取结算单数据、发票数据及收货行数据

  "获取结算单数据
  SELECT
    A~JSDDH,
    B~JSDHH,
    B~ZYSFPHM
    INTO TABLE @DATA(LT_JSD_EXIST)
    FROM ZFIT079_HEAD AS A
    JOIN ZFIT079_ITEM AS B
      ON A~JSDDH = B~JSDDH
     FOR ALL ENTRIES IN @GT_UPLOAD_DATA
   WHERE B~ZYSFPHM = @GT_UPLOAD_DATA-ZFPHM "上传文件ZFPHM最开始存的是原始发票号
     AND A~ZYWLX <> @GC_ZYWLX_FOREIGN
     AND A~JSDZT <> @GC_JSDZT_DEPRECATED.

  "获取发票数据
  SELECT *
    INTO TABLE @DATA(LT_ZFIT078_HEAD)
    FROM ZFIT078_HEAD
     FOR ALL ENTRIES IN @GT_UPLOAD_DATA
   WHERE ZFPHM = @GT_UPLOAD_DATA-ZFPHM.

  GT_ZFIT078_HEAD[] = LT_ZFIT078_HEAD[].

  LOOP AT GT_UPLOAD_DATA WHERE EBELN IS NOT INITIAL.
    CLEAR LT_R_EBELN.
    LT_R_EBELN-SIGN = 'I'.
    LT_R_EBELN-OPTION = 'EQ'.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = GT_UPLOAD_DATA-EBELN
      IMPORTING
        OUTPUT = LT_R_EBELN-LOW.
    APPEND LT_R_EBELN.

    IF GT_UPLOAD_DATA-CHARG IS NOT INITIAL.
      CLEAR LT_R_CHARG.
      LT_R_CHARG-SIGN = 'I'.
      LT_R_CHARG-OPTION = 'EQ'.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          INPUT  = GT_UPLOAD_DATA-CHARG
        IMPORTING
          OUTPUT = LT_R_CHARG-LOW.
      APPEND LT_R_CHARG.
    ENDIF.
  ENDLOOP.

  SORT LT_R_EBELN.
  DELETE ADJACENT DUPLICATES FROM LT_R_EBELN.
  SORT LT_R_CHARG.
  DELETE ADJACENT DUPLICATES FROM LT_R_CHARG.

  IF LT_R_EBELN[] IS NOT INITIAL.
    "取收货数据
    PERFORM FRM_GET_DATA_GR(ZFIU019) IF FOUND
       USING
          SPACE     "不排除结算单中存在的物料凭证
          LV_JSDDH     "当前结算单为空
          LV_WAERS
          LT_R_BUKRS[]
          LT_R_LIFNR[]
          LT_R_BSART[]
          LT_R_EKGRP[]
          LT_R_EBELN[]
          LT_R_EBELP[]
          LT_R_EBENP[]
          LT_R_WERKS[]
          LT_R_MATNR[]
          LT_R_GJAHR[]
          LT_R_BELNR[]
          LT_R_CHARG[]
          LT_R_ERNAM[]
          LT_R_AEDAT[]
          '1'
          'X'
          'X'
          'X'
          'X'
      CHANGING GT_GR_QUERY_RESULT[] GS_MSG.
  ENDIF.

  "2.以第一步取得的数据，更新上传数据的发票票面数据；
  "因税码有可能调整，更新收货行税率、税额，同时有调整的行项目金额、税码设置红色背景
  SORT LT_ZFIT078_HEAD BY ZFPHM.
  SORT GT_GR_QUERY_RESULT BY EBELN EBELP MBLNR MJAHR ZEILE SHEET_NO COND_TYPE.
  LOOP AT GT_UPLOAD_DATA.
    CLEAR LT_COLOR.
    CLEAR LS_COLOR.

    "以数据库里面的票面金额为准
    READ TABLE LT_ZFIT078_HEAD INTO DATA(LS_ZFIT078_HEAD) WITH KEY
      ZFPHM = GT_UPLOAD_DATA-ZFPHM
      BINARY SEARCH.
    IF SY-SUBRC = 0.
      "供应商显示为票面的供应商
      IF LS_ZFIT078_HEAD-LIFNR IS NOT INITIAL.
        GT_UPLOAD_DATA-LIFNR_AND_NAME = LS_ZFIT078_HEAD-LIFNR && GC_CONCAT_FLAG && LS_ZFIT078_HEAD-ZXFMC.
      ELSE.
        GT_UPLOAD_DATA-LIFNR_AND_NAME = LS_ZFIT078_HEAD-ZXFMC.
      ENDIF.

      GT_UPLOAD_DATA-ZHSJE = LS_ZFIT078_HEAD-ZHSJE.
      GT_UPLOAD_DATA-ZBHSJE = LS_ZFIT078_HEAD-ZBHSJE.
      GT_UPLOAD_DATA-ZHJSE = LS_ZFIT078_HEAD-ZHJSE.
    ENDIF.

    CLEAR GT_GR_QUERY_RESULT.
    READ TABLE GT_GR_QUERY_RESULT WITH KEY
      EBELN = GT_UPLOAD_DATA-EBELN
      EBELP = GT_UPLOAD_DATA-EBELP
      MBLNR = GT_UPLOAD_DATA-MBLNR
      MJAHR = GT_UPLOAD_DATA-MJAHR
      ZEILE = GT_UPLOAD_DATA-ZEILE
      SHEET_NO = GT_UPLOAD_DATA-SHEET_NO
      COND_TYPE = GT_UPLOAD_DATA-COND_TYPE.
    IF SY-SUBRC = 0.
      GT_UPLOAD_DATA-SHKZG = GT_GR_QUERY_RESULT-SHKZG.

      IF GT_UPLOAD_DATA-WRBTR <> GT_GR_QUERY_RESULT-WRBTR OR GT_UPLOAD_DATA-MWSKZ <> GT_GR_QUERY_RESULT-MWSKZ.
        IF GT_UPLOAD_DATA-WRBTR <> GT_GR_QUERY_RESULT-WRBTR.
          LS_COLOR-FNAME     = 'WRBTR'.
          LS_COLOR-COLOR-COL = COL_NEGATIVE.
          LS_COLOR-COLOR-INT = 0.
          LS_COLOR-COLOR-INV = 0.
          APPEND LS_COLOR TO LT_COLOR.
        ENDIF.

        IF GT_UPLOAD_DATA-MWSKZ <> GT_GR_QUERY_RESULT-MWSKZ.
          LS_COLOR-FNAME     = 'MWSKZ'.
          LS_COLOR-COLOR-COL = COL_NEGATIVE.
          LS_COLOR-COLOR-INT = 0.
          LS_COLOR-COLOR-INV = 0.
          APPEND LS_COLOR TO LT_COLOR.
        ENDIF.
        GT_UPLOAD_DATA-T_COLOR = LT_COLOR.
      ENDIF.
    ENDIF.

    PERFORM FRM_GET_TAX_RATE USING 'CN' GT_UPLOAD_DATA-MWSKZ CHANGING GT_UPLOAD_DATA-ZFPSL LV_TAXEXIST.
    GT_UPLOAD_DATA-ZFPSE = GT_UPLOAD_DATA-WRBTR * GT_UPLOAD_DATA-ZFPSL / 100.

    MODIFY GT_UPLOAD_DATA.
    CLEAR LS_ZFIT078_HEAD.
  ENDLOOP.

*3.合并处理开始*********************************************************************
  LT_UPLOAD_TMP[] = GT_UPLOAD_DATA[].
  DELETE LT_UPLOAD_TMP WHERE ZHBBS IS INITIAL.

  "有合并的情况
  IF LT_UPLOAD_TMP[] IS NOT INITIAL.
    "填充合并数据到结果表
    LOOP AT LT_UPLOAD_TMP.
      CLEAR LT_RESULT_HB.
      MOVE-CORRESPONDING LT_UPLOAD_TMP TO LT_RESULT_HB.
      APPEND LT_RESULT_HB.
    ENDLOOP.

    "第一步：删除重复的发票用于合计票面金额
    SORT LT_UPLOAD_TMP BY ZHBBS ZFPHM.
    DELETE ADJACENT DUPLICATES FROM LT_UPLOAD_TMP COMPARING ZHBBS ZFPHM.

    "第二步：合计票面金额
    LOOP AT LT_UPLOAD_TMP ASSIGNING FIELD-SYMBOL(<FS_UPLOAD_TMP>).
      LT_UPLOAD_TMP1-ZHSJE = LT_UPLOAD_TMP1-ZHSJE + <FS_UPLOAD_TMP>-ZHSJE.
      LT_UPLOAD_TMP1-ZHJSE = LT_UPLOAD_TMP1-ZHJSE + <FS_UPLOAD_TMP>-ZHJSE.
      LT_UPLOAD_TMP1-ZBHSJE  = LT_UPLOAD_TMP1-ZBHSJE + <FS_UPLOAD_TMP>-ZBHSJE.

      AT NEW ZHBBS.
        LV_HB_ZFPHM_FIRST = <FS_UPLOAD_TMP>-ZFPHM.
      ENDAT.

      AT END OF ZHBBS.
        LT_UPLOAD_TMP1-ZHBBS = <FS_UPLOAD_TMP>-ZHBBS.
        LV_NUM2 = LV_NUM2 + 1.
        "借助该字段放系统生成的虚拟发票号码
        LT_UPLOAD_TMP1-TXZ01 = LV_HB_ZFPHM_FIRST && GC_CONCAT_FLAG && GC_HB_FLAG && LV_NUM2.

        APPEND LT_UPLOAD_TMP1.
        CLEAR LT_UPLOAD_TMP1.
      ENDAT.
    ENDLOOP.

    "第三步：更新合并后的发票数据
    SORT LT_UPLOAD_TMP1 BY ZHBBS.
    LOOP AT LT_RESULT_HB.

      LT_RESULT_HB-ZYSFPHM = LT_RESULT_HB-ZFPHM. "原始发票号放合并之前的发票号码

      CLEAR LT_UPLOAD_TMP1.
      READ TABLE LT_UPLOAD_TMP1 WITH KEY
       ZHBBS = LT_RESULT_HB-ZHBBS BINARY SEARCH.

      LT_RESULT_HB-ZFPHM = LT_UPLOAD_TMP1-TXZ01 .
      LT_RESULT_HB-ZHSJE = LT_UPLOAD_TMP1-ZHSJE.
      LT_RESULT_HB-ZHJSE  = LT_UPLOAD_TMP1-ZHJSE .
      LT_RESULT_HB-ZBHSJE = LT_UPLOAD_TMP1-ZBHSJE.

      MODIFY LT_RESULT_HB.
    ENDLOOP.

    APPEND LINES OF LT_RESULT_HB TO LT_RESULT.
  ENDIF.

**合并处理结束********************************************************************

  "添加未合并的记录
  LOOP AT GT_UPLOAD_DATA WHERE ZHBBS IS INITIAL.
    CLEAR LT_RESULT.
    MOVE-CORRESPONDING GT_UPLOAD_DATA TO LT_RESULT.
    APPEND LT_RESULT.
  ENDLOOP.

  SORT LT_RESULT BY ZFPHM ZYSFPHM EBELN EBELP COND_TYPE MBLNR MJAHR ZEILE SHEET_NO .

**差额自动分摊处理开始***************************************************************
  IF P_AUTFT IS NOT INITIAL.
    LT_RESULT_TMP[] = LT_RESULT[].
    "DELETE LT_RESULT_TMP WHERE BSART_AND_TEXT CS 'TB'. "退货订单不参与分摊
    CLEAR:
     LV_MENGE,
     LV_WRBTR,
     LV_ZBHSJE,
     LV_ZBHSJE_SUM.

    LOOP AT LT_RESULT_TMP.
      IF LT_RESULT_TMP-SHKZG = 'S'.
        LV_ZBHSJE = LV_ZBHSJE + LT_RESULT_TMP-WRBTR.
      ELSE.
        LV_ZBHSJE = LV_ZBHSJE - LT_RESULT_TMP-WRBTR.
      ENDIF.

      IF LT_RESULT_TMP-COND_TYPE IS INITIAL. "AND LT_RESULT_TMP-BSART_AND_TEXT NS 'TB'. "退货的数量不参与分摊
        IF LT_RESULT_TMP-SHKZG = 'S'.
          LV_MENGE = LV_MENGE + LT_RESULT_TMP-ERFMG. "按数量比例分摊
          LV_WRBTR = LV_WRBTR + LT_RESULT_TMP-WRBTR. "按金额比例分摊
        ELSE.
          LV_MENGE = LV_MENGE - LT_RESULT_TMP-ERFMG.
          LV_WRBTR = LV_WRBTR - LT_RESULT_TMP-WRBTR.
        ENDIF.
      ENDIF.

      LS_RESULT_TMP = LT_RESULT_TMP.
      AT END OF ZFPHM.
        "LV_ZBHSJE = LS_RESULT_TMP-ZBHSJE - LV_ZBHSJE - P_GLAMT / '1.13'. "需分摊的金额
        LV_ZBHSJE = LS_RESULT_TMP-ZBHSJE - LV_ZBHSJE - P_GLAMT. "需分摊的金额
        IF LV_ZBHSJE <> 0. "有差额需自动分摊

          CLEAR LV_ZBHSJE_SUM.
          LOOP AT LT_RESULT ASSIGNING FIELD-SYMBOL(<FS_RESULT>) WHERE ZFPHM = LS_RESULT_TMP-ZFPHM
            AND COND_TYPE IS INITIAL AND EBELN IS NOT INITIAL. "AND BSART_AND_TEXT NS 'TB'.

            IF P_QTYFT IS NOT INITIAL.
              <FS_RESULT>-BEZNK = LV_ZBHSJE * <FS_RESULT>-ERFMG / LV_MENGE. "按数量比例分摊
            ELSEIF P_AMTFT IS NOT INITIAL.
              <FS_RESULT>-BEZNK = LV_ZBHSJE * <FS_RESULT>-WRBTR / LV_WRBTR. "按金额比例分摊
            ENDIF.

            IF <FS_RESULT>-SHKZG = 'S'.
              LV_ZBHSJE_SUM = LV_ZBHSJE_SUM + <FS_RESULT>-BEZNK.
            ELSE.
              LV_ZBHSJE_SUM = LV_ZBHSJE_SUM - <FS_RESULT>-BEZNK.
            ENDIF.

            <FS_RESULT>-ZFPSE = ( <FS_RESULT>-WRBTR + <FS_RESULT>-BEZNK ) * <FS_RESULT>-ZFPSL / 100.
          ENDLOOP.

          LV_ZBHSJE = LV_ZBHSJE - LV_ZBHSJE_SUM.
          IF LV_ZBHSJE <> 0. "未分摊完的金额
            LOOP AT LT_RESULT ASSIGNING <FS_RESULT> WHERE ZFPHM = LS_RESULT_TMP-ZFPHM
              AND COND_TYPE IS INITIAL AND EBELN IS NOT INITIAL. "AND BSART_AND_TEXT NS 'TB'.

              <FS_RESULT>-BEZNK = <FS_RESULT>-BEZNK + LV_ZBHSJE.
              <FS_RESULT>-ZFPSE = ( <FS_RESULT>-WRBTR + <FS_RESULT>-BEZNK ) * <FS_RESULT>-ZFPSL / 100.
              EXIT.
            ENDLOOP.
          ENDIF.

          CLEAR:
           LV_MENGE,
           LV_WRBTR,
           LV_ZBHSJE,
           LV_ZBHSJE_SUM.

        ENDIF.
      ENDAT.
    ENDLOOP.

  ENDIF.
**差额自动分摊处理结束***************************************************************

*4.拆分处理开始**********************************************************************
  REFRESH LT_RESULT_TMP[].
  IF P_SPLNUM > 0 AND P_SPLNUM <= 450.

    LOOP AT LT_RESULT WHERE BSART_AND_TEXT CS 'Z3'. "委外加工
      P_SPLNUM = 200.  "一行收货对3行财务凭证
      EXIT.
    ENDLOOP.

    "第一步：取出需拆分的发票数据，并计算每张发票需拆分几次
    LOOP AT LT_RESULT ASSIGNING <FS_RESULT>.
      LV_COUNT = LV_COUNT + 1.
      AT END OF ZFPHM.
        IF LV_COUNT > P_SPLNUM.

          LT_RESULT_TMP = <FS_RESULT>.
          "需拆分次数记录到该字段
          LT_RESULT_TMP-ZHJSE_GX = CEIL( LV_COUNT / P_SPLNUM ).
          APPEND LT_RESULT_TMP.
        ENDIF.
        CLEAR LV_COUNT.
      ENDAT.
    ENDLOOP.

    IF LT_RESULT_TMP[] IS NOT INITIAL."需拆分的发票数据
      "第二步：更新发票号码
      CLEAR LV_COUNT.
      LV_NUM2 = 1.
      LOOP AT LT_RESULT_TMP.
        LOOP AT LT_RESULT WHERE ZFPHM = LT_RESULT_TMP-ZFPHM.
          LV_TABIX = SY-TABIX.
          LV_COUNT = LV_COUNT + 1.

          LT_RESULT_CF = LT_RESULT.
          "无合并情况，原始发票号放拆分之前的发票号码,有合并情况该值已经为原始发票号
          IF LT_RESULT_CF-ZYSFPHM IS INITIAL.
            LT_RESULT_CF-ZYSFPHM = LT_RESULT-ZFPHM.
          ENDIF.
          LT_RESULT_CF-ZHBBS = LT_RESULT-ZFPHM.  "合并标识放原始发票号，供下一步使用
          LT_RESULT_CF-ZFPHM = LT_RESULT-ZFPHM && GC_CONCAT_FLAG && GC_CF_FLAG && LV_NUM2.
          APPEND LT_RESULT_CF.

          IF LV_COUNT >= P_SPLNUM.
            IF LV_COUNT MOD P_SPLNUM = 0.
              LV_NUM2 = LV_NUM2 + 1.
            ENDIF.
          ENDIF.

          AT END OF ZFPHM.
            LV_NUM2 = 1.
            CLEAR LV_COUNT.
          ENDAT.

          DELETE LT_RESULT INDEX LV_TABIX.
        ENDLOOP.
      ENDLOOP.

      "第三步：根据拆分后的发票号码，合计收货行项目金额到虚拟发票金额
      CLEAR:
        LV_ZHJSE,
        LV_ZBHSJE,
        LT_RESULT_TMP.
      REFRESH LT_RESULT_TMP[].

      "算出拆分后每张发票的票面金额
      SORT LT_RESULT_CF BY ZHBBS ZFPHM.
      LOOP AT LT_RESULT_CF ASSIGNING <FS_RESULT>.
        CLEAR LV_CF_ZFPHM_LAST.

        "合计行项目税额、不含税金额
        IF <FS_RESULT>-SHKZG = 'H'.
          LV_ZHJSE = LV_ZHJSE - <FS_RESULT>-ZFPSE.    "拆分的每张发票税额合计
          LV_ZBHSJE  = LV_ZBHSJE - <FS_RESULT>-WRBTR - <FS_RESULT>-BEZNK. "拆分的每张发票不含税金额合计
        ELSE.
          LV_ZHJSE = LV_ZHJSE + <FS_RESULT>-ZFPSE.    "拆分的每张发票税额合计
          LV_ZBHSJE  = LV_ZBHSJE + <FS_RESULT>-WRBTR + <FS_RESULT>-BEZNK. "拆分的每张发票不含税金额合计
        ENDIF.

        AT END OF ZHBBS. "拆分前的发票号码
          LV_CF_ZFPHM_LAST = <FS_RESULT>-ZFPHM.
        ENDAT.

        AT END OF ZFPHM."拆分后的发票号码
          LT_RESULT_TMP-ZFPHM = <FS_RESULT>-ZFPHM.

          IF LV_CF_ZFPHM_LAST IS NOT INITIAL."拆分后的最后一张发票, 金额为票面金额减去之前已经合计的行项目金额
            LT_RESULT_TMP-ZHJSE  =  <FS_RESULT>-ZHJSE - LV_ZFPSE_SUM .
            LT_RESULT_TMP-ZBHSJE =  <FS_RESULT>-ZBHSJE - LV_WRBTR_SUM .
          ELSE.
            LT_RESULT_TMP-ZHJSE = LV_ZHJSE.                        "抬头合计税额
            LT_RESULT_TMP-ZBHSJE = LV_ZBHSJE.                      "抬头不含税金额
          ENDIF.

          LV_ZFPSE_SUM = LV_ZFPSE_SUM + LT_RESULT_TMP-ZHJSE. "拆分的所有发票税额合计
          LV_WRBTR_SUM = LV_WRBTR_SUM + LT_RESULT_TMP-ZBHSJE. "拆分的所有发票不含税金额合计

          LT_RESULT_TMP-ZHSJE = LT_RESULT_TMP-ZHJSE + LT_RESULT_TMP-ZBHSJE.            "抬头含税总金额
          APPEND LT_RESULT_TMP.

          CLEAR:
           LV_ZHJSE,
           LV_ZBHSJE,
           LT_RESULT_TMP.
        ENDAT.


        AT END OF ZHBBS.
          CLEAR:
            LV_ZFPSE_SUM,
            LV_WRBTR_SUM,
            LV_CF_ZFPHM_LAST.

        ENDAT.
      ENDLOOP.

      "第四步：更新最终拆分虚拟发票金额
      SORT LT_RESULT_TMP BY ZFPHM.
      LOOP AT LT_RESULT_CF.
        LV_TABIX = SY-TABIX.

        CLEAR LT_RESULT_TMP.
        READ TABLE LT_RESULT_TMP WITH KEY
          ZFPHM = LT_RESULT_CF-ZFPHM
          BINARY SEARCH.

        IF SY-SUBRC = 0.
          CLEAR LT_RESULT_CF-ZHBBS.
          LT_RESULT_CF-ZHJSE = LT_RESULT_TMP-ZHJSE.
          LT_RESULT_CF-ZBHSJE = LT_RESULT_TMP-ZBHSJE.
          LT_RESULT_CF-ZHSJE = LT_RESULT_TMP-ZHSJE.
        ENDIF.

        MODIFY LT_RESULT_CF INDEX LV_TABIX.
      ENDLOOP.
      APPEND LINES OF LT_RESULT_CF TO LT_RESULT.

    ENDIF.
  ENDIF.
*拆分处理结束**********************************************************************


  "5.填充ALV显示数据，并更新勾选合计金额，数据检查规则应用
  CLEAR:
    LV_ZHJSE,
    LT_RESULT_TMP.
  REFRESH LT_RESULT_TMP[].

  SORT LT_RESULT BY ZFPHM.
  LOOP AT LT_RESULT ASSIGNING <FS_RESULT>.

    IF <FS_RESULT>-SHKZG = 'H'. "和退货订单一起开票的情况
      LV_ZHJSE = LV_ZHJSE - <FS_RESULT>-ZFPSE.
      LT_RESULT_TMP-ZBHSJE_GX  = LT_RESULT_TMP-ZBHSJE_GX - <FS_RESULT>-WRBTR - <FS_RESULT>-BEZNK.
    ELSE.
      LV_ZHJSE = LV_ZHJSE + <FS_RESULT>-ZFPSE.
      LT_RESULT_TMP-ZBHSJE_GX  = LT_RESULT_TMP-ZBHSJE_GX + <FS_RESULT>-WRBTR + <FS_RESULT>-BEZNK.
    ENDIF.

    AT END OF ZFPHM.
      LT_RESULT_TMP-ZFPHM = <FS_RESULT>-ZFPHM.
      LT_RESULT_TMP-ZHJSE_GX = LV_ZHJSE.
      APPEND LT_RESULT_TMP.

      CLEAR:
        LV_ZHJSE,
        LT_RESULT_TMP.
    ENDAT.
  ENDLOOP.

  SORT LT_JSD_EXIST BY ZYSFPHM.
  SORT LT_RESULT_TMP BY ZFPHM.
  LOOP AT LT_RESULT.
    LV_TABIX = SY-TABIX.
    IF LT_RESULT-ZYSFPHM IS INITIAL.
      LT_RESULT-ZYSFPHM = LT_RESULT-ZFPHM.
    ENDIF.

    CLEAR LT_RESULT_TMP.
    READ TABLE LT_RESULT_TMP WITH KEY
      ZFPHM = LT_RESULT-ZFPHM
      BINARY SEARCH.
    IF SY-SUBRC = 0.
      LT_RESULT-ZHJSE_GX = LT_RESULT_TMP-ZHJSE_GX.
      LT_RESULT-ZBHSJE_GX = LT_RESULT_TMP-ZBHSJE_GX.
    ENDIF.

    "数据检查
    "项目文本必填
    IF LT_RESULT-SGTXT IS INITIAL.
      LT_RESULT-LIGHT = ICON_RED_LIGHT.
      LT_RESULT-MSG = TEXT-M17.
      REPLACE ALL OCCURRENCES OF '&' IN LT_RESULT-MSG WITH LT_RESULT-ZYSFPHM.
    ENDIF.

    IF LT_RESULT-LIGHT IS INITIAL.
      "发票号要存在
      CLEAR LS_ZFIT078_HEAD.
      READ TABLE LT_ZFIT078_HEAD INTO LS_ZFIT078_HEAD WITH KEY
       ZFPHM = LT_RESULT-ZYSFPHM
       BINARY SEARCH.
      IF SY-SUBRC <> 0.
        LT_RESULT-LIGHT = ICON_RED_LIGHT.
        LT_RESULT-MSG = TEXT-M17.
        REPLACE ALL OCCURRENCES OF '&' IN LT_RESULT-MSG WITH LT_RESULT-ZYSFPHM.

      ELSE.
        "购方名称要正确
        CONDENSE: LS_ZFIT078_HEAD-ZGFMC.
        IF LS_ZFIT078_HEAD-ZGFMC <> '华菱安赛乐米塔尔汽车板有限公司'.
          LT_RESULT-LIGHT = ICON_RED_LIGHT.
          LT_RESULT-MSG = TEXT-M21.
          REPLACE ALL OCCURRENCES OF '&' IN LT_RESULT-MSG WITH LT_RESULT-ZYSFPHM.
        ENDIF.
      ENDIF.
    ENDIF.

    "拆分后的发票金额不能是负数
    IF LT_RESULT-LIGHT IS INITIAL AND LT_RESULT-ZBHSJE < 0.
      LT_RESULT-LIGHT = ICON_RED_LIGHT.
      LT_RESULT-MSG = TEXT-M22.
    ENDIF.



    "收货行项目要存在
    IF LT_RESULT-LIGHT IS INITIAL AND LT_RESULT-EBELN IS NOT INITIAL.
      CLEAR GT_GR_QUERY_RESULT.
      READ TABLE GT_GR_QUERY_RESULT WITH KEY
        EBELN = LT_RESULT-EBELN
        EBELP = LT_RESULT-EBELP
        MBLNR = LT_RESULT-MBLNR
        MJAHR = LT_RESULT-MJAHR
        ZEILE = LT_RESULT-ZEILE
        SHEET_NO = LT_RESULT-SHEET_NO
        COND_TYPE = LT_RESULT-COND_TYPE.
      IF SY-SUBRC <> 0.
        LT_RESULT-LIGHT = ICON_RED_LIGHT.
        LT_RESULT-MSG = TEXT-M18.
      ELSE.
        "收货行不能做过结算单检查
        IF GT_GR_QUERY_RESULT-JSDDH IS NOT INITIAL.
          LT_RESULT-LIGHT = ICON_RED_LIGHT.
          LT_RESULT-MSG = TEXT-M12. "该收货行在结算单&中存在
          REPLACE ALL OCCURRENCES OF '&' IN LT_RESULT-MSG WITH GT_GR_QUERY_RESULT-JSDDH.
        ENDIF.
      ENDIF.
    ENDIF.

    "发票号码不能做过结算单检查
    IF LT_RESULT-LIGHT IS INITIAL.
      READ TABLE LT_JSD_EXIST INTO DATA(LS_JSD_EXIST)
        WITH KEY ZYSFPHM = LT_RESULT-ZYSFPHM
        BINARY SEARCH.
      IF SY-SUBRC = 0.
        LT_RESULT-LIGHT = ICON_RED_LIGHT.
        LT_RESULT-MSG = TEXT-M10.
        REPLACE ALL OCCURRENCES OF '&' IN LT_RESULT-MSG WITH LS_JSD_EXIST-JSDDH.
      ENDIF.
    ENDIF.

    "行项目金额加分摊金额需大于零
    IF LT_RESULT-LIGHT IS INITIAL AND LT_RESULT-EBELN IS NOT INITIAL..
      IF LT_RESULT-WRBTR + LT_RESULT-BEZNK <= 0.
        LT_RESULT-LIGHT = ICON_RED_LIGHT.
        LT_RESULT-MSG = TEXT-M20.
      ENDIF.
    ENDIF.

    "票面供应商和PO供应商不一致
    IF LT_RESULT-LIGHT IS INITIAL AND LT_RESULT-EBELN IS NOT INITIAL..
      IF LS_ZFIT078_HEAD-LIFNR <> GT_GR_QUERY_RESULT-LIFNR.
        LT_RESULT-LIGHT = ICON_YELLOW_LIGHT.
        LT_RESULT-MSG = '票面供应商和PO供应商不一致'(M13).
      ENDIF.
    ENDIF.

    "勾选金额和发票金额不一致
    IF LT_RESULT-LIGHT IS INITIAL.
      IF LT_RESULT-ZBHSJE <> LT_RESULT-ZBHSJE_GX OR LT_RESULT-ZHJSE <> LT_RESULT-ZHJSE_GX.
        LT_RESULT-LIGHT = ICON_YELLOW_LIGHT.
        LT_RESULT-MSG = '勾选的行项目收货金额和票面金额不一致'(M11).
      ENDIF.
    ENDIF.

    "以上检查完全通过
    IF LT_RESULT-LIGHT IS INITIAL.
      LT_RESULT-LIGHT = ICON_GREEN_LIGHT.
    ENDIF.

    MODIFY LT_RESULT INDEX LV_TABIX.
  ENDLOOP.

  GT_RESULT[] = LT_RESULT[].
  SORT GT_RESULT BY ZFPHM.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_ALV_DISPLAY .
  DATA:

    LR_DISPLAY   TYPE REF TO CL_SALV_DISPLAY_SETTINGS,
    LR_FUNC_LIST TYPE REF TO CL_SALV_FUNCTIONS_LIST,
    "LR_EVENTS    TYPE REF TO CL_SALV_EVENTS,
    LR_EVENTS    TYPE REF TO CL_SALV_EVENTS_TABLE,
    LR_SELECTS   TYPE REF TO CL_SALV_SELECTIONS,
    LR_COLUMNS   TYPE REF TO CL_SALV_COLUMNS_TABLE,
    LR_COLUMN    TYPE REF TO CL_SALV_COLUMN_TABLE,
    LR_LAYOUT    TYPE REF TO CL_SALV_LAYOUT,
    LS_DDIC      TYPE SALV_S_DDIC_REFERENCE,
    LS_KEY       TYPE SALV_S_LAYOUT_KEY.

  TRY.
      CL_SALV_TABLE=>FACTORY(
        IMPORTING
          R_SALV_TABLE = GR_TABLE
        CHANGING
          T_TABLE      = GT_RESULT[]
      ).
    CATCH CX_SALV_MSG.
  ENDTRY.

  "设置列Lable
  LR_COLUMNS = GR_TABLE->GET_COLUMNS( ).
  LR_COLUMNS->SET_OPTIMIZE( 'X' ).
  LR_COLUMNS->SET_KEY_FIXATION( 'X' ).

  TRY.
      LR_COLUMNS->SET_COLOR_COLUMN( 'T_COLOR' ).
    CATCH CX_SALV_DATA_ERROR.                           "#EC NO_HANDLER
  ENDTRY.

  "显示设置
  LR_DISPLAY = GR_TABLE->GET_DISPLAY_SETTINGS( ).
  LR_DISPLAY->SET_STRIPED_PATTERN( 'X' ). "设置颜色间隔显示

  "设置选择模式
  LR_SELECTS = GR_TABLE->GET_SELECTIONS( ).
  LR_SELECTS->SET_SELECTION_MODE( CL_SALV_SELECTIONS=>IF_SALV_C_SELECTION_MODE~CELL ).

  "STAUTS ALV 标准功能
*  LR_FUNC_LIST = GR_TABLE->GET_FUNCTIONS( ).
*  LR_FUNC_LIST->SET_ALL( 'X' ).

  "设置ALV自定义按钮功能
  GR_TABLE->SET_SCREEN_STATUS(
    PFSTATUS      = '9000'
    REPORT        = SY-REPID
    SET_FUNCTIONS = GR_TABLE->C_FUNCTIONS_ALL ).

  "按钮事件接受
  LR_EVENTS = GR_TABLE->GET_EVENT( ).
  CREATE OBJECT GR_EVENTS_HANDLER.
  SET HANDLER GR_EVENTS_HANDLER->ON_USER_COMMAND FOR LR_EVENTS.
  SET HANDLER GR_EVENTS_HANDLER->ON_LINK_CLICK FOR LR_EVENTS.


  "打开Layout保存功能
  LR_LAYOUT = GR_TABLE->GET_LAYOUT( ).
  LS_KEY-REPORT = SY-REPID.
  LR_LAYOUT->SET_KEY( LS_KEY ).
  LR_LAYOUT->SET_DEFAULT( ABAP_TRUE ).
  LR_LAYOUT->SET_SAVE_RESTRICTION( IF_SALV_C_LAYOUT=>RESTRICT_NONE ).

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'LIGHT' ).
      LR_COLUMN->SET_KEY( 'X' ).
      LR_COLUMN->SET_ICON( 'X' ).
      LR_COLUMN->SET_SHORT_TEXT( '状态灯'(H01) ).
      LR_COLUMN->SET_MEDIUM_TEXT( '状态灯'(H01) ).
      LR_COLUMN->SET_LONG_TEXT( '状态灯'(H01) ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'MSG' ).
      LR_COLUMN->SET_KEY( 'X' ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'ZFPHM' ).
      LR_COLUMN->SET_KEY( 'X' ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'EBELN' ).
      LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>HOTSPOT ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'JSDDH' ).
      LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>HOTSPOT ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'MBLNR' ).
      LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>HOTSPOT ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'ZHBBS' ).
      LR_COLUMN->SET_VISIBLE( ABAP_FALSE ).
      LR_COLUMN->SET_SHORT_TEXT( '合并标识'(H02) ).
      LR_COLUMN->SET_MEDIUM_TEXT( '合并标识'(H02) ).
      LR_COLUMN->SET_LONG_TEXT( '合并标识'(H02) ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'LIFNR_AND_NAME' ).
      LR_COLUMN->SET_SHORT_TEXT( '票面供应商'(H03) ).
      LR_COLUMN->SET_MEDIUM_TEXT( '票面供应商'(H03) ).
      LR_COLUMN->SET_LONG_TEXT( '票面供应商'(H03) ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'BSART_AND_TEXT' ).
      LR_COLUMN->SET_SHORT_TEXT( '采购订单类型'(H04) ).
      LR_COLUMN->SET_MEDIUM_TEXT( '采购订单类型'(H04) ).
      LR_COLUMN->SET_LONG_TEXT( '采购订单类型'(H04) ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

  TRY .
      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'WERKS_AND_NAME' ).
      LR_COLUMN->SET_SHORT_TEXT( '工厂'(H05) ).
      LR_COLUMN->SET_MEDIUM_TEXT( '工厂'(H05) ).
      LR_COLUMN->SET_LONG_TEXT( '工厂'(H05) ).
    CATCH CX_SALV_NOT_FOUND.
  ENDTRY.

*  TRY .
*      LR_COLUMN  ?= LR_COLUMNS->GET_COLUMN( 'ZYSFPHM' ).
*      LR_COLUMN->SET_VISIBLE( ABAP_FALSE ).
*    CATCH CX_SALV_NOT_FOUND.
*  ENDTRY.

  DATA: LS_COLOR TYPE LVC_S_COLO.
  TRY.
      LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'ZBHSJE_GX' ).
      LS_COLOR-COL = COL_POSITIVE.
      LS_COLOR-INT = 0.
      LS_COLOR-INV = 0.
      LR_COLUMN->SET_COLOR( LS_COLOR ).
    CATCH CX_SALV_NOT_FOUND.                            "#EC NO_HANDLER
  ENDTRY.

  TRY.
      LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'ZHJSE_GX' ).
      LS_COLOR-COL = COL_POSITIVE.
      LS_COLOR-INT = 0.
      LS_COLOR-INV = 0.
      LR_COLUMN->SET_COLOR( LS_COLOR ).
    CATCH CX_SALV_NOT_FOUND.                            "#EC NO_HANDLER
  ENDTRY.

  GR_TABLE->DISPLAY( ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_refresh_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_REFRESH_DATA .
  CLEAR:
     GS_MSG.

  REFRESH:
    GT_INV_DATA[],
    GT_GR_DATA[],
    GT_UPLOAD_DATA[],
    GT_GR_QUERY_RESULT[],
    GT_RESULT[].

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_BATCH_SAVE_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_BATCH_SAVE_SUBMIT_DATA USING IV_COMMAND.
  DATA:
    LV_NUM         TYPE I,
    LS_MSG         TYPE BAPIRET2,
    LS_JSD_HEAD    TYPE ZFIT079_HEAD,
    LT_JSD_HEAD    TYPE STANDARD TABLE OF ZFIT079_HEAD,
    LT_JSD_ITEM    TYPE TABLE OF ZFIT079_ITEM WITH HEADER LINE,
    LT_RESULT_TMP  TYPE TABLE OF TY_RESULT WITH HEADER LINE,
    LS_ZFIT078_HIS TYPE ZFIT078_HIS,
    LT_ZFIT078_HIS TYPE STANDARD TABLE OF ZFIT078_HIS.

  LOOP AT GT_RESULT WHERE LIGHT = ICON_RED_LIGHT AND JSDDH IS INITIAL.
    EXIT.
  ENDLOOP.
  IF SY-SUBRC = 0.
    MESSAGE '包含红灯出错的记录，不能批量保存'(M14) TYPE 'E'.
    RETURN.
  ENDIF.

  IF IV_COMMAND = '&SUBMIT'.
    GV_POPUP_MSG = '确认批量提交发票结算单数据？'(M15).
    CONDENSE GV_POPUP_MSG.

    CLEAR GV_POPUP_YES.
    PERFORM FRM_POPUP_TO_CONFIRM USING GV_POPUP_MSG CHANGING GV_POPUP_YES.
    IF GV_POPUP_YES IS INITIAL.
      RETURN.
    ENDIF.
  ENDIF.

  "结算单保存数据准备
  LOOP AT GT_RESULT WHERE JSDDH IS INITIAL.
    CLEAR:
    GT_RESULT-LIGHT,
    GT_RESULT-MSG,
    GT_RESULT-ZHBBS.

    MODIFY GT_RESULT.
  ENDLOOP.

  SORT GT_ZFIT078_HEAD BY ZFPHM.
  SORT GT_RESULT BY ZFPHM.
  LOOP AT GT_RESULT ASSIGNING FIELD-SYMBOL(<FS_RESULT>) WHERE JSDDH IS INITIAL.

    AT NEW ZFPHM.
      CLEAR LS_JSD_HEAD.
      LS_JSD_HEAD-ZYWLX = GC_ZYWLX_DOMESTIC.
      PERFORM FRM_GENERATE_JSDDH(ZFIU019) USING LS_JSD_HEAD-ZYWLX
            CHANGING LS_JSD_HEAD-JSDDH.
    ENDAT.

    LV_NUM = LV_NUM + 1.
    CLEAR LT_JSD_ITEM.

    CLEAR GT_GR_QUERY_RESULT.
    READ TABLE GT_GR_QUERY_RESULT WITH KEY
      EBELN = <FS_RESULT>-EBELN
      EBELP = <FS_RESULT>-EBELP
      MBLNR = <FS_RESULT>-MBLNR
      MJAHR = <FS_RESULT>-MJAHR
      ZEILE = <FS_RESULT>-ZEILE
      SHEET_NO = <FS_RESULT>-SHEET_NO
      COND_TYPE = <FS_RESULT>-COND_TYPE .
    IF SY-SUBRC = 0.
      MOVE-CORRESPONDING GT_GR_QUERY_RESULT TO LT_JSD_ITEM.
    ENDIF.

    LT_JSD_ITEM-JSDDH = LS_JSD_HEAD-JSDDH.
    LT_JSD_ITEM-JSDHH = LV_NUM.
    LT_JSD_ITEM-ZYSFPHM = <FS_RESULT>-ZYSFPHM.
    LT_JSD_ITEM-ERFMG = <FS_RESULT>-ERFMG.
    LT_JSD_ITEM-WRBTR = <FS_RESULT>-WRBTR.
    LT_JSD_ITEM-BEZNK = <FS_RESULT>-BEZNK.
    LT_JSD_ITEM-MWSKZ = <FS_RESULT>-MWSKZ.
    LT_JSD_ITEM-ZFPSL = <FS_RESULT>-ZFPSL.
    LT_JSD_ITEM-ZFPSE = <FS_RESULT>-ZFPSE.
    LT_JSD_ITEM-SHKZG = <FS_RESULT>-SHKZG.
    APPEND LT_JSD_ITEM.

    AT END OF ZFPHM.
      LS_JSD_HEAD-JSDLX = GC_JSDLX_PO.
      LS_JSD_HEAD-JSDZT = GC_JSDZT_DRAFT.
      LS_JSD_HEAD-ZFPHM = <FS_RESULT>-ZFPHM.
      LS_JSD_HEAD-SGTXT = <FS_RESULT>-SGTXT.
      LS_JSD_HEAD-BKTXT = LS_JSD_HEAD-ZFPHM.

      READ TABLE GT_ZFIT078_HEAD INTO DATA(LS_ZFIT078_HEAD) WITH KEY
        ZFPHM = <FS_RESULT>-ZYSFPHM
        BINARY SEARCH.
      IF SY-SUBRC = 0.
        "LS_JSD_HEAD-ZFPLX = LS_ZFIT078_HEAD-ZFPLX.
        LS_JSD_HEAD-ZKPSJ = LS_ZFIT078_HEAD-ZKPSJ.
        LS_JSD_HEAD-BUKRS = LS_ZFIT078_HEAD-BUKRS.
        LS_JSD_HEAD-ZGFMC = LS_ZFIT078_HEAD-ZGFMC.
        LS_JSD_HEAD-LIFNR = LS_ZFIT078_HEAD-LIFNR.
        LS_JSD_HEAD-ZXFMC = LS_ZFIT078_HEAD-ZXFMC.

        IF LS_ZFIT078_HEAD-INVOICE_STATUS CS '红冲'.
          LS_JSD_HEAD-VORGANG = GC_YWCL_SUBSC. "发票事务 后续贷记
        ELSE.
          LS_JSD_HEAD-VORGANG = GC_YWCL_INV.
        ENDIF.


      ENDIF.

      LS_JSD_HEAD-ZHSJE     = <FS_RESULT>-ZHSJE.
      LS_JSD_HEAD-ZHJSE     = <FS_RESULT>-ZHJSE.
      LS_JSD_HEAD-ZBHSJE    = <FS_RESULT>-ZBHSJE.
      LS_JSD_HEAD-ZHJSE_GX  = <FS_RESULT>-ZHJSE_GX.
      LS_JSD_HEAD-ZBHSJE_GX = <FS_RESULT>-ZBHSJE_GX.

      LS_JSD_HEAD-ZHJSE_CY  = LS_JSD_HEAD-ZHJSE - <FS_RESULT>-ZHJSE_GX.
      LS_JSD_HEAD-ZBHSJE_CY = LS_JSD_HEAD-ZBHSJE - <FS_RESULT>-ZBHSJE_GX.

      LS_JSD_HEAD-WAERS     = 'CNY'.
      LS_JSD_HEAD-ZPLCJ_FLG = 'X'.
      LS_JSD_HEAD-CRNAM = SY-UNAME.
      LS_JSD_HEAD-CRDAT = SY-DATUM.
      LS_JSD_HEAD-CRTIM = SY-UZEIT.

      "保存发票处理历史
      IF LT_JSD_ITEM[] IS NOT INITIAL.
        LOOP AT LT_JSD_ITEM.
          CLEAR LS_ZFIT078_HIS.
          LS_ZFIT078_HIS-ZFPHM = LT_JSD_ITEM-ZYSFPHM.
          LS_ZFIT078_HIS-CHNAM = SY-UNAME.
          LS_ZFIT078_HIS-CHDAT = SY-DATUM.
          LS_ZFIT078_HIS-CHTIM = SY-UZEIT.
          LS_ZFIT078_HIS-ZFPZT = '20'.  "结算单处理中
          LS_ZFIT078_HIS-JSDDH = LT_JSD_ITEM-JSDDH.
          APPEND LS_ZFIT078_HIS TO LT_ZFIT078_HIS.
        ENDLOOP.

        SORT LT_ZFIT078_HIS.
        DELETE ADJACENT DUPLICATES FROM LT_ZFIT078_HIS COMPARING ALL FIELDS.
      ENDIF.

      MODIFY ZFIT079_HEAD FROM LS_JSD_HEAD.
      MODIFY ZFIT079_ITEM FROM TABLE LT_JSD_ITEM[].
      IF LT_ZFIT078_HIS[] IS NOT INITIAL.
        MODIFY ZFIT078_HIS FROM TABLE LT_ZFIT078_HIS.
      ENDIF.

      COMMIT WORK AND WAIT.
      IF SY-SUBRC = 0.
        CLEAR LT_RESULT_TMP.
        LT_RESULT_TMP-ZFPHM = <FS_RESULT>-ZFPHM.
        LT_RESULT_TMP-JSDDH = LS_JSD_HEAD-JSDDH.
        LT_RESULT_TMP-JSDZT = LS_JSD_HEAD-JSDZT.
        LT_RESULT_TMP-LIGHT = ICON_GREEN_LIGHT.
        LT_RESULT_TMP-MSG = '结算单保存成功'(M16).
        APPEND LT_RESULT_TMP.
      ENDIF.

      CLEAR:
        LV_NUM,LS_JSD_HEAD.
      REFRESH LT_JSD_ITEM[].
    ENDAT.
  ENDLOOP.

  SORT LT_RESULT_TMP BY ZFPHM.
  LOOP AT GT_RESULT WHERE JSDDH IS INITIAL.
    READ TABLE LT_RESULT_TMP WITH KEY ZFPHM = GT_RESULT-ZFPHM
      BINARY SEARCH.
    IF SY-SUBRC = 0.
      GT_RESULT-JSDDH = LT_RESULT_TMP-JSDDH.
      GT_RESULT-JSDZT = LT_RESULT_TMP-JSDZT.
      GT_RESULT-LIGHT = LT_RESULT_TMP-LIGHT.
      GT_RESULT-MSG = LT_RESULT_TMP-MSG.
    ENDIF.
    MODIFY GT_RESULT.
  ENDLOOP.

  "结算单数据提交
  IF IV_COMMAND = '&SUBMIT'.
    IF GT_RESULT[] IS NOT INITIAL.
      SELECT  *
        FROM ZFIT079_HEAD
        INTO TABLE LT_JSD_HEAD
         FOR ALL ENTRIES IN GT_RESULT
       WHERE JSDDH = GT_RESULT-JSDDH.

      IF SY-SUBRC = 0.
        SORT LT_JSD_HEAD BY JSDDH.
        SORT GT_RESULT BY JSDDH.
        REFRESH LT_RESULT_TMP[].

        LOOP AT GT_RESULT ASSIGNING <FS_RESULT> WHERE JSDDH IS NOT INITIAL
          AND JSDZT = GC_JSDZT_DRAFT.

          AT END OF JSDDH.
            CLEAR LS_JSD_HEAD.
            READ TABLE LT_JSD_HEAD INTO LS_JSD_HEAD
              WITH KEY JSDDH = <FS_RESULT>-JSDDH
              BINARY SEARCH.

            IF SY-SUBRC = 0.
              CLEAR LS_ZFIT078_HEAD.
              READ TABLE GT_ZFIT078_HEAD INTO LS_ZFIT078_HEAD WITH KEY
               ZFPHM = <FS_RESULT>-ZYSFPHM
               BINARY SEARCH.

              "附件同步
              PERFORM FRM_SYNCFILE(ZFIU019) IF FOUND
                USING LS_JSD_HEAD 'X'.

              "IDOC 触发
              CLEAR LS_MSG.
              PERFORM FRM_IDOC_TRIGGER(ZFIU019) IF FOUND
                USING LS_ZFIT078_HEAD CHANGING LS_JSD_HEAD LS_MSG.

              CLEAR LT_RESULT_TMP.
              LT_RESULT_TMP-ZFPHM = LS_JSD_HEAD-ZFPHM.
              LT_RESULT_TMP-JSDDH = LS_JSD_HEAD-JSDDH.
              LT_RESULT_TMP-JSDZT = LS_JSD_HEAD-JSDZT.
              IF LS_MSG-TYPE = 'S'.
                LT_RESULT_TMP-LIGHT = ICON_GREEN_LIGHT.
              ELSE.
                LT_RESULT_TMP-LIGHT = ICON_RED_LIGHT.
              ENDIF.

              LT_RESULT_TMP-MSG = LS_MSG-MESSAGE.
              APPEND LT_RESULT_TMP.

              WAIT UP TO 5 SECONDS. "VIM过账会锁，需要等待

            ENDIF.
          ENDAT.
        ENDLOOP.

        SORT LT_RESULT_TMP BY JSDDH.
        LOOP AT GT_RESULT WHERE JSDDH IS NOT INITIAL AND JSDZT = GC_JSDZT_DRAFT.
          READ TABLE LT_RESULT_TMP WITH KEY JSDDH = GT_RESULT-JSDDH
            BINARY SEARCH.
          IF SY-SUBRC = 0.
            GT_RESULT-JSDZT = LT_RESULT_TMP-JSDZT.
            GT_RESULT-LIGHT = LT_RESULT_TMP-LIGHT.
            GT_RESULT-MSG = LT_RESULT_TMP-MSG.
          ENDIF.
          MODIFY GT_RESULT.
        ENDLOOP.

      ENDIF.

    ENDIF.
  ENDIF.


  DATA:
    LR_COLUMNS    TYPE REF TO CL_SALV_COLUMNS_TABLE.
  LR_COLUMNS = GR_TABLE->GET_COLUMNS( ).
  LR_COLUMNS->SET_OPTIMIZE( 'X' ).
  GR_TABLE->REFRESH( ).

ENDFORM.

FORM FRM_POPUP_TO_CONFIRM  USING    IV_POPUP_MSG
                       CHANGING CV_POPUP_YES.
  CLEAR CV_POPUP_YES.
  DATA LV_ANSWER.

  CALL FUNCTION 'POPUP_TO_CONFIRM'
    EXPORTING
      TITLEBAR              = IV_POPUP_MSG
      TEXT_QUESTION         = IV_POPUP_MSG
      DISPLAY_CANCEL_BUTTON = SPACE
    IMPORTING
      ANSWER                = LV_ANSWER
    EXCEPTIONS
      TEXT_NOT_FOUND        = 1
      OTHERS                = 2.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    RETURN.
  ENDIF.

  IF LV_ANSWER = '1'.
    CV_POPUP_YES = 'X'.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form ZFPHM_F4_VALUES
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM ZFPHM_F4_VALUES USING P_FIELD.

  DATA:
    LT_R_LIFNR      TYPE RANGE OF ZFIT078_HEAD-LIFNR WITH HEADER LINE,
    LT_R_BUKRS      TYPE RANGE OF ZFIT078_HEAD-BUKRS WITH HEADER LINE,
    LV_TABIX        TYPE I,
    LT_ZFIT078_HEAD TYPE TABLE OF ZFIT078_HEAD WITH HEADER LINE.

  DATA:
    LS_DYNPRO_VALUES TYPE  DYNPREAD,
    LT_DYNPRO_VALUES TYPE  STANDARD TABLE OF DYNPREAD.

  DATA:
    LV_BUKRS TYPE ZFIT079_HEAD-BUKRS,
    LV_LIFNR TYPE ZFIT079_HEAD-LIFNR.

  LS_DYNPRO_VALUES-FIELDNAME = 'S_LIFNR-LOW'.
  APPEND LS_DYNPRO_VALUES TO LT_DYNPRO_VALUES  .
  LS_DYNPRO_VALUES-FIELDNAME = 'S_BUKRS-LOW'.
  APPEND LS_DYNPRO_VALUES TO LT_DYNPRO_VALUES  .

  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      DYNAME               = SY-REPID
      DYNUMB               = SY-DYNNR
      TRANSLATE_TO_UPPER   = 'X'
    TABLES
      DYNPFIELDS           = LT_DYNPRO_VALUES
    EXCEPTIONS
      INVALID_ABAPWORKAREA = 1
      INVALID_DYNPROFIELD  = 2
      INVALID_DYNPRONAME   = 3
      INVALID_DYNPRONUMMER = 4
      INVALID_REQUEST      = 5
      NO_FIELDDESCRIPTION  = 6
      INVALID_PARAMETER    = 7
      UNDEFIND_ERROR       = 8
      DOUBLE_CONVERSION    = 9
      STEPL_NOT_FOUND      = 10
      OTHERS               = 11.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  CLEAR: LS_DYNPRO_VALUES.
  READ TABLE LT_DYNPRO_VALUES INTO LS_DYNPRO_VALUES WITH KEY FIELDNAME = 'S_BUKRS-LOW'.
  LV_BUKRS = LS_DYNPRO_VALUES-FIELDVALUE.

  CLEAR: LS_DYNPRO_VALUES.
  READ TABLE LT_DYNPRO_VALUES INTO LS_DYNPRO_VALUES WITH KEY FIELDNAME = 'S_LIFNR-LOW'.
  LV_LIFNR = LS_DYNPRO_VALUES-FIELDVALUE.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      INPUT  = LV_LIFNR
    IMPORTING
      OUTPUT = LV_LIFNR.

  IF LV_LIFNR IS NOT INITIAL.
    CLEAR LT_R_LIFNR.
    LT_R_LIFNR-SIGN = 'I'.
    LT_R_LIFNR-OPTION = 'EQ'.
    LT_R_LIFNR-LOW = LV_LIFNR .
    APPEND LT_R_LIFNR.
  ENDIF.

  IF LV_BUKRS IS NOT INITIAL.
    CLEAR LT_R_BUKRS.
    LT_R_BUKRS-SIGN = 'I'.
    LT_R_BUKRS-OPTION = 'EQ'.
    LT_R_BUKRS-LOW = LV_BUKRS.
    APPEND LT_R_BUKRS.
  ENDIF.

  "获取发票数据
  SELECT *
    INTO TABLE LT_ZFIT078_HEAD
    FROM ZFIT078_HEAD
   WHERE LIFNR IN LT_R_LIFNR
     AND BUKRS IN LT_R_BUKRS.

  "排除结算单中已经存在的发票号码
  SELECT
    A~JSDDH,
    B~JSDHH,
    B~ZYSFPHM
    INTO TABLE @DATA(LT_JSD_EXIST)
    FROM ZFIT079_HEAD AS A
    JOIN ZFIT079_ITEM AS B
      ON A~JSDDH = B~JSDDH
     FOR ALL ENTRIES IN @LT_ZFIT078_HEAD
   WHERE B~ZYSFPHM = @LT_ZFIT078_HEAD-ZFPHM
     AND A~ZYWLX <> @GC_ZYWLX_FOREIGN
     AND A~JSDZT <> @GC_JSDZT_DEPRECATED.

  SORT LT_JSD_EXIST BY ZYSFPHM.
  DELETE ADJACENT DUPLICATES FROM LT_JSD_EXIST COMPARING ZYSFPHM.

  LOOP AT LT_ZFIT078_HEAD.
    LV_TABIX = SY-TABIX.

    READ TABLE LT_JSD_EXIST TRANSPORTING NO FIELDS
      WITH KEY ZYSFPHM = LT_ZFIT078_HEAD-ZFPHM
      BINARY SEARCH.
    IF SY-SUBRC = 0 .
      DELETE LT_ZFIT078_HEAD INDEX LV_TABIX.
    ENDIF.
  ENDLOOP.

  SORT LT_ZFIT078_HEAD BY BUKRS LIFNR ZXFMC ZKPSJ ZFPHM.

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      RETFIELD         = 'ZFPHM'
      DYNPPROG         = SY-REPID
      DYNPNR           = SY-DYNNR
      DYNPROFIELD      = P_FIELD
      VALUE_ORG        = 'S'
      CALLBACK_PROGRAM = SY-REPID
    TABLES
      VALUE_TAB        = LT_ZFIT078_HEAD
    EXCEPTIONS
      PARAMETER_ERROR  = 1
      NO_VALUES_FOUND  = 2
      OTHERS           = 3.
  IF SY-SUBRC <> 0.
*    MESSAGE ID SY-MSGID TYPE 'E' NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


ENDFORM.
